import { getNewsListApi } from '../../api/news';

Page({
  data: {
    size: 0,
    limit: 8,
    newsList: [],
    loading: false,
    notMoreData: false,
  },

  /** 获取新闻列表 */
  getNewsList() {
    if (this.data.notMoreData) return;
    this.setData({
      loading: true,
    });
    getNewsListApi({ size: this.data.size, limit: this.data.limit })
      .then((res) => {
        this.data.newsList.push(...res);
        this.setData({
          newsList: this.data.newsList,
          size: this.data.size + this.data.limit,
          notMoreData: !res.length,
        });
      })
      .catch((error) => {
        console.log(error);
      })
      .finally(() => {
        this.setData({
          loading: false,
        });
      });
  },

  onReachBottom() {
    console.log('触底');
    this.getNewsList();
  },

  onLoad(options) {
    this.getNewsList();
  },
});
